package roborally.program.conditions;

import roborally.model.Element;
import roborally.model.Position;
import roborally.model.Robot;

/**
 * A class for dealing with can-hit-robot-conditions: (can-hit-robot).  
 * 
 * @version	1.0
 * @author  Pieter Robberechts	(1e Bach Informatica)
 * @author	Jeroen Sanders		(1e Bach Informatica)
 *
 */
public class CanHitRobot extends Condition {

	/**
	 * Initialize this new can-hit-robot-condition with a line number.
	 * 
	 */
	public CanHitRobot(int lineNb) {
		super(lineNb);
	}

	/**
	 * Evaluate this condition for a given robot.
	 * 
	 * @return	Return true if and only if the given robot can hit another robot
	 * 			in its current state; else return false.
	 * 			| ...
	 */
	@Override
	public boolean evaluate(Robot robot) {
		if( ! super.evaluate(robot))
			return false;
		
		Position nextPositionWithItem = robot.getBoard().getFirstElementStartingAt(
				robot.getPosition(), robot.getOrientation()).getPosition();
		while(nextPositionWithItem != null) {
			Iterable<Element> elementsOnPosition = robot.getBoard().getElements(nextPositionWithItem);
			for(Element element : elementsOnPosition)
				if(element instanceof Robot)
					return true;
		}	
		return false;
	}
	
	/**
	 * Return a textual representation of this can-hit-robot-condition,
	 * formatted in accordance with its position in the program.
	 * 
	 * @return	The result is a pretty formatted string of this can-hit-robot-condition,
	 * 			taking in account his relative position in the program.
	 * 			| result == getIndentation(indentationLevel) + "(can-hit-robot)"
	 */
	@Override
	public String prettyFormat(int indentationLevel) {
		return getIndentation(indentationLevel) + "(can-hit-robot)";
	}

}
